﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DSPL.DataAccess;
using System.Windows;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DSPL.Services.Rules
{
    public class StockRulesService
    {

        // Stock balance FIFO
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dtStockTable">Temporary table - Items Data Table</param>
        /// <param name="dtLocationTable">Temporary table - Location Data Table</param>
        /// <param name="fromDate">From Date</param>
        /// <param name="toDate">To Date</param>
        /// <param name="dtStockBalFifo"></param>
        /// <param name="transactionType">Transaction Types</param>
        /// <param name="dispatchType">Dispatch Types</param>
        /// <param name="reportTableGroup"></param>
        /// <param name="originalItemsTable"></param>
        /// <param name="originalStockIn">Original Stock In Table - fStkIn</param>
        /// <returns>Creates a temporary cursor TmpStockFifo in the database which need to be destroyed in the calling method.</returns>
        public bool StockBalanceFifo(DataTable dtStockTable, DataTable dtLocationTable, DateTime fromDate, DateTime toDate, out DataTable dtStockBalFifo,
            string transactionType = "", string dispatchType = "", string reportTableGroup = "", string originalItemsTable = "", string originalStockIn = "")
        {

            dtStockBalFifo = new DataTable();
            if (dtStockTable == null || dtStockTable.Rows.Count == 0)
            {
                return false;
            }

            if (dtLocationTable == null || dtLocationTable.Rows.Count == 0)
            {
                return false;
            }

            if (fromDate == null)
            {
                return false;
            }

            bool isOpenCal = false;
            if (toDate == null)
            {
                toDate = fromDate;
            }
            else
            {
                isOpenCal = true;
            }

            bool isSuccess = true;
            return isSuccess;

            var db = DatabaseAccess.Instance;
            List<ParameterWithValue> paramList = new List<ParameterWithValue>();
            paramList.Add(new ParameterWithValue("@tableItems", dtStockTable));
            paramList.Add(new ParameterWithValue("@tableLocation", dtLocationTable));
            paramList.Add(new ParameterWithValue("@dateFrom", fromDate));

            paramList.Add(new ParameterWithValue("@dateTo", toDate));
            paramList.Add(new ParameterWithValue("@vchTransactionType", transactionType));
            paramList.Add(new ParameterWithValue("@vchDispatchType", dispatchType));
            paramList.Add(new ParameterWithValue("@vchItemTable", originalItemsTable));
            paramList.Add(new ParameterWithValue("@vchStockInTable", originalStockIn));

            //  paramList.Add(new ParameterWithValue("@tableStockBalFifo",dtStockBalFifo,, ParameterDirection.InputOutput));
            paramList.Add(new ParameterWithValue("@result", true, SqlDbType.Structured, ParameterDirection.InputOutput));

            SpCaller spCaller = new SpCaller() { SpName = "StockBalanceFifo", SpParamValues = paramList.ToArray() };
            if (db.Excecute(spCaller, "@tableStockBalFifo") == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

      

        /// <summary>
        /// Validates Stock Transaction Date
        /// </summary>
        /// <param name="transactionDate"> Transaction Date</param>
        /// <param name="referenceNo">Reference No</param>
        /// <param name="checkBackDated">whether to check back dates transactions or not i.e. true or false</param>
        /// <param name="reportGroup">Report Group Name</param>
        /// <returns>true or false. Updates the errorList if any error occurs</returns>
        public bool ValidateStockTransactionDate(DateTime transactionDate, string referenceNo = "", bool checkBackDated = false, string reportGroup = "")
        {

            if (transactionDate == null)
            {
                return false;
            }

            var db = DatabaseAccess.Instance;
            bool isSuccess ;
            string message="";
            List<ParameterWithValue> paramList = new List<ParameterWithValue>();

            //paramList.Add(new ParameterWithValue("@dateTransaction", transactionDate));
            //paramList.Add(new ParameterWithValue("@vchReferenceNo", referenceNo));
            //paramList.Add(new ParameterWithValue("@result", true, SqlDbType.Bit, ParameterDirection.Output));
         //   paramList.Add(new ParameterWithValue("@message", message, SqlDbType.VarChar, ParameterDirection.Output));
            paramList.Add(new ParameterWithValue("@TxnDate", transactionDate));
            paramList.Add(new ParameterWithValue("@ReferenceNo", referenceNo));
            paramList.Add(new ParameterWithValue("@IsCheckBackDate", true));
            paramList.Add(new ParameterWithValue("@ReturnValue", 0, SqlDbType.Bit, ParameterDirection.Output));
         //   paramList.Add(new ParameterWithValue("@message", message, SqlDbType.VarChar, ParameterDirection.Output));
            
            SpCaller spCaller = new SpCaller() { SpName = "Stock.VALIDATE_STOCK_TXNDATE", SpParamValues = paramList.ToArray() };
           // SpCaller spCaller = new SpCaller() { SpName = "ValidateStockTxnDate", SpParamValues = paramList.ToArray() };
            isSuccess = (bool)(db.Excecute(spCaller, "@ReturnValue"));
            
            return isSuccess;            

        }

         #region Fifo_In_Stk_Upd

        // Multiple Line FIFO Stock Receive and QOH Update 
        /// <summary>
        /// Update Backend Location Stock Balance table and insert record to FIFO Receive table.
        /// </summary>
        /// <param name="stockInHandTable">Location wise stock In hand table (fItemsLoc)</param>
        /// <param name="stockReceiveTable">FIFO Receive table (fStkIn)</param>
        /// <param name="dtTranDet">Transaction Detail table - temporary table</param>
        /// <param name="transactionDate">Transaction Date</param>
        /// <param name="referenceNo">Transaction Reference No</param>
        /// <param name="locCode">Location Code </param>
        /// <param name="tranactionType">Transaction Type</param> 
        /// <param name="productMasterTable">Product Master Table</param>
        /// <param name="TransactionDetailTable">Original Transaction Detail Table</param>
        /// <param name="isAverageUpdate">Average Update Flag (true – Update, false – Omit)</param>
        /// <param name="linkRefernceNo">Link Transaction Reference (Po reference no in GRN)</param>
        /// <param name="linkTranactionDetailTable">Link Transaction Detail Table</param> 
        /// <returns></returns>
        public bool MultipleFifoStockReceive(string stockInHandTable, string stockReceiveTable, DataTable dtTranDet, DateTime transactionDate,
            string referenceNo, string locCode, string tranactionType, string productMasterTable,
            bool isAverageUpdate = false, string linkRefernceNo = "", string linkTranactionDetailTable = "", string TransactionDetailTable = "", SqlTransaction sqlTransaction = null)
        {

            if (stockInHandTable == String.Empty)
            {
                return false;
            }

            if (stockReceiveTable == String.Empty)
            {
                return false;
            }

            if ((dtTranDet == null) || (dtTranDet.Rows.Count == 0))
            {
                return false;
            }

            if (referenceNo == String.Empty)
            {
                return false;
            }

            if (locCode == String.Empty)
            {
                return false;
            }

            if (tranactionType == String.Empty)
            {
                return false;
            }

            if (isAverageUpdate)
            {
                if (productMasterTable == string.Empty)
                {
                    return false;
                }
            }

            if (linkRefernceNo != string.Empty && linkTranactionDetailTable == string.Empty)
            {
                return false;
            }


            return true;
        }
 #endregion

    }
}
